package com.homework.Impl;

import com.homework.dao.AuthCodeMapper;
import homework.code.CodeService;
import homework.facade.dto.AuthCode;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Component
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private AuthCodeMapper authCodeMapper;

    @Override
    public Integer checkAuthCode(String email, String code) {
        AuthCode authCode = authCodeMapper.selectAuthCode(email);
        if (ObjectUtils.isEmpty(authCode)) {
            return 1;
        }
        if (authCode.getCode().equals(code) ) {
            Date expiretime = authCode.getExpiretime();
            if (expiretime.after(new Date())) {
                return 0;
            }
        }
        return 1;
    }
}
